package com.muyu.danmo.service.impl;


import com.muyu.common.core.enums.VipEquity;
import com.muyu.common.security.utils.ApplyColorToImage;
import com.muyu.common.security.utils.SecurityUtils;
import com.muyu.common.system.domain.LoginUser;
import com.muyu.danmo.domain.DanmoBackPicture;
import com.muyu.danmo.mapper.DanmoBackPictureMapper;
import com.muyu.danmo.service.IDanmoBackPictureService;
import com.muyu.remote.RemoteVipPerson;

import com.muyu.system.domain.DanmoVipRights;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Base64;
import java.util.List;



/**
 * 背景图片Service业务层处理
 * 
 * @author yangle
 * @date 2025-03-08
 */
@Service
@Log4j2
public class DanmoBackPictureServiceImpl implements IDanmoBackPictureService
{
    @Autowired
    private DanmoBackPictureMapper danmoBackPictureMapper;

    /**
     * 查询背景图片
     * 
     * @param id 背景图片主键
     * @return 背景图片
     */
    @Override
    public DanmoBackPicture selectDanmoBackPictureById(Long id)
    {
        return danmoBackPictureMapper.selectDanmoBackPictureById(id);
    }
    @Autowired
    private RemoteVipPerson remoteVipPerson;
    /**
     * 查询背景图片列表
     * 
     * @param danmoBackPicture 背景图片
     * @return 背景图片
     */
    @Override
    public List<DanmoBackPicture> selectDanmoBackPictureList(DanmoBackPicture danmoBackPicture)
    {

        LoginUser loginUser = SecurityUtils.getLoginUser();
        Long userid = loginUser.getUserid();
        List<DanmoVipRights> personRights = remoteVipPerson.getPersonRights(userid);
        List<DanmoBackPicture> danmoBackPictures = danmoBackPictureMapper.selectDanmoBackPictureList(danmoBackPicture);

// 检查用户是否有VIP权限
        boolean hasVipRights = false;
        if (personRights != null) {
            for (DanmoVipRights personRight : personRights) {
                if (personRight.getVipInfoId() == VipEquity.FRAME.getRights() ||
                        personRight.getVipInfoId() == VipEquity.SEAL_SHAPE.getCode()) {
                    hasVipRights = true;
                    break;
                }
            }
        }

// 处理背景图片颜色
        if (danmoBackPicture.getColor() != null && danmoBackPictures != null) {
            for (DanmoBackPicture picture : danmoBackPictures) {
                processImage(picture, danmoBackPicture.getColor());
            }
        }

        if (danmoBackPictures != null && !danmoBackPictures.isEmpty()) {
            for (int i = 0; i < danmoBackPictures.size(); i++) {
                DanmoBackPicture picture = danmoBackPictures.get(i);
                // 前三条设置为免费
                if (i < 3) {
                    picture.setC("0");
                }
                // 其余根据VIP权限设置
                else {
                    picture.setC("1");
                }
            }
        }

        return danmoBackPictures;

    }
    public static void processImage(DanmoBackPicture fontInfo, String color) {
        String imageUrl = fontInfo.getPicture();
        // 1. 移除前缀 "data:image/png;base64,"
        if (imageUrl.startsWith("data:image/png;base64,")) {
            imageUrl = imageUrl.substring("data:image/png;base64,".length());
        }

        try {
            // 2. 解码 Base64 字符串为字节数组
            byte[] originalImageBytes = Base64.getDecoder().decode(imageUrl);

            // 3. 转换为 BufferedImage
            ByteArrayInputStream bis = new ByteArrayInputStream(originalImageBytes);
            BufferedImage originalImage = ImageIO.read(bis);

            // 4. 颜色渲染
            BufferedImage coloredImage = ApplyColorToImage.applyColorToImage(originalImage,color);

            // 5. 转换为 Base64 字符串
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            ImageIO.write(coloredImage, "png", bos);
            String coloredImageBase64 = Base64.getEncoder().encodeToString(bos.toByteArray());

            // 6. 添加前缀 "data:image/png;base64," 并更新图片
            fontInfo.setPicture("data:image/png;base64," + coloredImageBase64);
        } catch (IOException e) {
            // 7. 处理错误，保留原图片 Base64
            fontInfo.setPicture("data:image/png;base64," + imageUrl); // 保留原 Base64 以便回退
        }
    }
    /**
     * 新增背景图片
     * 
     * @param danmoBackPicture 背景图片
     * @return 结果
     */
    @Override
    public int insertDanmoBackPicture(DanmoBackPicture danmoBackPicture)
    {
        return danmoBackPictureMapper.insertDanmoBackPicture(danmoBackPicture);
    }

    /**
     * 修改背景图片
     * 
     * @param danmoBackPicture 背景图片
     * @return 结果
     */
    @Override
    public int updateDanmoBackPicture(DanmoBackPicture danmoBackPicture)
    {
        return danmoBackPictureMapper.updateDanmoBackPicture(danmoBackPicture);
    }

    /**
     * 批量删除背景图片
     * 
     * @param ids 需要删除的背景图片主键
     * @return 结果
     */
    @Override
    public int deleteDanmoBackPictureByIds(Long[] ids)
    {
        return danmoBackPictureMapper.deleteDanmoBackPictureByIds(ids);
    }

    /**
     * 删除背景图片信息
     * 
     * @param id 背景图片主键
     * @return 结果
     */
    @Override
    public int deleteDanmoBackPictureById(Long id)
    {
        return danmoBackPictureMapper.deleteDanmoBackPictureById(id);
    }

    @Override
    public DanmoBackPicture getPicture(DanmoBackPicture danmoBackPicture) {
        DanmoBackPicture danmoBackPictureById = danmoBackPictureMapper.selectDanmoBackPictureById(danmoBackPicture.getId());
        if (danmoBackPicture.getColor()!=null){
            processImage(danmoBackPictureById,danmoBackPicture.getColor());
            return danmoBackPictureById;
        }else {
            return danmoBackPictureById;
        }


    }


}
